package com.example.controller;

import com.example.POJO.Account;
import com.example.POJO.Product;
import com.example.POJO.User;
import com.example.service.ProductService;
import com.google.gson.Gson;
import org.codehaus.plexus.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService productService;
//    @PostMapping(value = "/Query", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @PostMapping(value = "/Query", consumes = "application/json;charset=UTF-8")
    public String searchProduct(@RequestBody HashMap<String, String> data) {
        try {
            Product product=new Product();
            String description=data.get("description");
            String kind=data.get("kind");
            String tag=data.get("tag");
            String name=data.get("name");
            product.setDescription(description);
            product.setKind(kind);
            product.setTag(tag);
            product.setName(name);
            List<Product> products=productService.getProductBySearch(product);
            Gson gson = new Gson();
            String res = gson.toJson(products);
            return res;

        } catch (Exception e) {
            // 处理异常，返回相应的错误响应
            System.out.println(e);
            return "";
        }
    }
    @GetMapping(value = "/Query", consumes = "application/json;charset=UTF-8")
    public String searchProduct2() {
        try {
            Product product=new Product();
            product.setDescription("");
            product.setKind("");
            product.setTag("");
            product.setName("");
            List<Product> products=productService.getProductBySearch(product);
            Gson gson = new Gson();
            String res = gson.toJson(products);
            return res;

        } catch (Exception e) {
            // 处理异常，返回相应的错误响应
            System.out.println(e);
            return "";
        }
    }
}
